<template>
    <div class="game-all-data-box" v-loading="loading">
        <div class="input-box" style="position: relative">
            <!-- <el-button type="success" class="btn" @click="exportData" style="position: absolute; right: 10px">导出</el-button> -->
            <div class="user-search">
                <span class="input-title">用户查询</span>
                <!-- <el-tooltip style="cursor:pointer; margin-right: 10px" class="item" effect="dark" placement="top">
                    <div slot="content">可输入 用户ID、手机号、用户昵称、<br>身份证号、托管过的QQ号 来查询、姓名。</div>
                    <i class="el-icon-question"></i>
                </el-tooltip> -->
                <el-input
                    placeholder=""
                    clearable
                    @keyup.enter.native="getList(1)"
                    style="width:500px;"
                    v-model.trim="keyword">
                    <i slot="prefix" class="el-input__icon el-icon-search"></i>
                </el-input>
                <span class="user-card">
                    <span>可输入 趣ID、手机号、用户昵称、姓名、身份证号、托管过的QQ号 来查询</span>
                </span>
            </div>
            <el-button type="primary" class="btn" @click="getList(1)">搜索</el-button>
            
            <span class="input-title" style="margin-left: 10px;">注册时间</span>
            <el-date-picker
                @change="getList(1)"
                style="width: 300px;"
                v-model="date"
                type="daterange"
                value-format="yyyy-MM-dd"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
            </el-date-picker>

        </div>
        <div class="input-wrap">
            <div class="input-in">
                <span style="margin-right: 10px; font-size: 14px; font-weight: 500;">是否注册</span>
                <el-select @change="loginChange" style="width:150px;" v-model="loginChangeVal">
                    <el-option label="全部" value=""></el-option>
                    <el-option label="已注册" :value="0"></el-option>
                    <el-option label="未注册" :value="1"></el-option>
                </el-select>
            </div>
            <div class="input-in">
                <span style="margin:0px 10px; font-size: 14px; font-weight: 500;">状态</span>
                <el-select @change="statusChange" style="width:150px;" v-model="statusChangeVal">
                    <el-option label="全部" value=""></el-option>
                    <el-option label="正常用户" :value="1"></el-option>
                    <el-option label="不可提现" :value="0"></el-option>
                    <el-option label="处罚中" :value="10"></el-option>
                    <!-- <el-option label="封禁" :value="-1"></el-option>
                    <el-option label="钱包冻结" :value="-2"></el-option> -->
                    <el-option label="已注销" :value="-3"></el-option>
                </el-select>
            </div>
            <div class="input-in">
                <span style="margin: 0 10px; font-size: 14px; font-weight: 500;">角色</span>
                <el-select @change="roleChange" style="width:150px;" v-model="roleChangeVal">
                    <el-option label="全部" :value='null'></el-option>
                    <el-option label="平台运营" :value="1"></el-option>
                    <el-option label="交易顾问" :value="2"></el-option>
                </el-select>
            </div>
        </div>
        <div class="table-box">
            <el-table
                :data="list"
                border
                @sort-change="sortChange"
                style="width: 100%">
                <el-table-column
                    fixed
                    prop="userId"
                    width="120"
                    label="用户趣ID">
                    <template slot-scope="scope">
                        <div @click="goUserDetail(scope.row.userId)" class="table-btn">{{scope.row.userId}}</div>
                        <div style="color: red" v-html="scope.row.blackDesc.replace(' ', '<br>')"></div>
                    </template>
                </el-table-column>
                <el-table-column
                    width="120"
                    label="昵称">
                    <template slot-scope="scope">
                        <span>{{scope.row.nickname || '-'}}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    width="200"
                    prop="createTime"
                    label="注册时间">
                    <template slot-scope="scope">
                        <div>{{scope.row.createTime | timeInit}}</div>
                        <div>
                            <b>登录方式：</b>
                            <span>{{scope.row.loginType || '-'}}</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column
                    label="来源渠道"
                    width="160"
                    >
                    <template slot-scope="scope">
                        <span>{{scope.row.channel || '-'}}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    label="状态">
                    <template slot-scope="scope">
                        <span :style="{ 'color': scope.row.status === 10 && 'red'}">{{statusVal[scope.row.status + ''] || '-'}}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    label="角色">
                    <template slot-scope="scope">
                        <p v-for="(role, index) in scope.row.roleNames" :key="index">{{role || '-'}}</p>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="totalRecharge"
                    sortable="custom"
                    label="充值金额">
                    <template slot-scope="scope">
                        <span>{{scope.row.totalRecharge || 0}} 元</span>
                    </template>
                </el-table-column>
                <el-table-column
                    sortable="custom"
                    prop="createOrderCount"
                    label="租号次数">
                    <template slot-scope="scope">
                        <span>{{scope.row.createOrderCount || 0}}次</span>
                    </template>
                </el-table-column>
                <el-table-column
                    sortable="custom"
                    prop="applyDiamondCount"
                    label="租号花费">
                    <template slot-scope="scope">
                        <span>{{scope.row.applyDiamondCount || 0}} 元</span>
                    </template>
                </el-table-column>
                <el-table-column
                    sortable="custom"
                    prop="rentAccountNum"
                    label="在租账号">
                    <template slot-scope="scope">
                        <span>{{scope.row.rentAccountNum || 0}} 个</span>
                    </template>
                </el-table-column>
                <el-table-column
                    sortable="custom"
                    prop="orderIncome"
                    label="出租收益">
                    <template slot-scope="scope">
                        <span>{{scope.row.orderIncome || 0}} 元</span>
                    </template>
                </el-table-column>
                <el-table-column
                    sortable="custom"
                    prop="sellAccountNum"
                    label="在售账号">
                    <template slot-scope="scope">
                        <span>{{scope.row.sellAccountNum || 0}}个</span>
                    </template>
                </el-table-column>
                <el-table-column
                    sortable="custom"
                    prop="hasGetAccount"
                    label="已购账号">
                    <template slot-scope="scope">
                        <span>{{scope.row.hasGetAccount || '-'}}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    sortable="custom"
                    prop="hasSellAccount"
                    label="已售账号">
                    <template slot-scope="scope">
                        <span>{{scope.row.hasSellAccount || '-'}}</span>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="tools">
            <el-pagination
                @size-change="sizeChange"
                @current-change="pageChange"
                :current-page="pageNum"
                :page-sizes="_pageSizes"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
import { formatDate, conversion } from '@/utils/utils'
import { export2Excel } from '@/excel/util'
export default {
    data () {
        return {
            loading: false,
            list: [],
            pageNum: 1,
            pageSize: this._pageSize,
            total: 0,
            keyword: '',
            userId: '',
            date: [], // 时间
            type: '',
            order: '',
            loginChangeVal: '',
            statusChangeVal: '',
            statusVal: {
                '1': '正常用户',
                '0': '不可提现',
                '10': '处罚中',
                '-3': '已注销'
            },
            roleChangeVal: null
        }
    },
    created () {
        this.keyword = this.$route.query.keyword || ''
        console.log(this.keyword, 'keyword')
        this.getList(1)
    },

    methods: {
        exportData() {
            this.columns = [{
                title: '用户趣ID', key: 'userId'
            },
            // {
            //     title: '昵称', key: 'nickname'
            // },
            {
                title: '注册时间', key: 'createTimeAndLoginType'
            },
            {
                title: '登录方式', key: 'loginType'
            },
            {
                title: '状态', key: 'statusDesc'
            },
            {
                title: '角色', key: 'roleNames'
            },
            {
                title: '充值金额（元）', key: 'totalRecharge'
            },
            {
                title: '租号次数', key: 'createOrderCount'
            },
            {
                title: '租号花费（元）', key: 'applyDiamondCount'
            },
            {
                title: '在租账号（个）', key: 'rentAccountNum'
            },
            {
                title: '出租收益（元）', key: 'orderIncome'
            },
            {
                title: '在售账号（个）', key: 'sellAccountNum'
            },
            {
                title: '已购账号', key: 'hasGetAccount'
            },
            {
                title: '已售账号', key: 'hasSellAccount'
            }
        ]
        this.getList(1, true)
        },
        roleChange(val) {
            this.roleChangeVal = val
            this.getList(1)
        },
        loginChange(val) {
            this.loginChangeVal = val
            this.getList(1)
        },
        statusChange(val) {
            this.statusChangeVal = val
            this.getList(1)
        },
        async getList (num, flag) {
            if (this.loading) return
            this.loading = true
            this.pageNum = num

            let params = {
                pageNum: this.pageNum,
                pageSize: this.pageSize,
                loginType: this.loginChangeVal,
                status: this.statusChangeVal,
                roleStatus: this.roleChangeVal,
            }

            if (this.type) params.type = this.type
            if (this.sort) params.sort = this.sort

            if (this.date && this.date.length) {
                params.fromDate = this.date[0]
                params.toDate = this.date[1]
            }

            if (this.keyword) {
                params.keyword = this.keyword
            }
            // 打印
            if (flag) {
                params.pageSize = 1000000
            }
            let res = await this.$http.get('/data/selectUserDetailData', {
                params
            })
            if (res.code === '0') {
                if (flag) {
                    res.data && res.data.forEach((item) => {
                        item.statusDesc = this.statusVal[item.status]
                        item.createTimeAndLoginType = formatDate(item.createTime, 'YYYY-MM-DD HH:MM:SS')
                    })
                    export2Excel(this.columns, res.data, '用户列表')
                } else {
                        res.data && res.data.forEach(item => {
                            item.userJoinTimesVoList = item.userJoinTimesVoList || []
                        })
                        this.list = res.data
                        this.total = res.count
                }

            } else {
                this.$message({
                    type: 'error',
                    message: res.desc
                })
            }
            this.loading = false
        },

        sizeChange (val) {
            this.pageSize = val
            this.getList(1)
        },

        pageChange (val) {
            this.getList(val)
        },

        // 后端排序
        sortChange ({ column, prop, order }) {
            console.log(prop, '000000')
            this.sort = order || ''
            // 如果没有 sort prop 置为空
            if (!this.sort) {
                this.type = null
            } else {
                if (this.sort === 'ascending') {
                    this.sort = 'asc'
                } else {
                    this.sort = 'desc'
                }
                this.type = prop
            }
            this.getList(1)
        },

        // 查看用户详情
        goUserDetail (id) {
            const { href } = this.$router.resolve({
                path: `/dataReport/userInfo`,
                query: {
                    id
                }
            })
            window.open(href, '_blank')
        },

        // 数字转换
        conversion (val) {
            return conversion(val)
        }
    },

    filters: {
        timeInit (val) {
            if (val) {
                return formatDate(val, 'YYYY-MM-DD')
            } else {
                return '-'
            }
        }
    }
}
</script>

<style lang="scss" scoped>
    .game-all-data-box {
        .input-wrap {
            display: flex;
            justify-content: flex-start;
            align-items: center;
            margin-top: 30px;
            margin-bottom: 20px;
            .input-in {
                display: flex;
                justify-content: flex-start;
                align-items: center;
            }
        }
        .input-title {
            margin-right: 10px;
        }
        .input-box {
            margin-top: 10px;
            font-size: 13px;
            font-weight: 500;
            margin-bottom: 60px;
            display: flex;
            justify-content: flex-start;
            align-items: center;
            .user-search {
                position: relative;
                .user-card {
                    position: absolute;
                    bottom: -60px;
                    left: 40px;
                    display: inline-block;
                    border-radius: 4px;
                    border: solid 1px #00ba45;
                    background-color: #eefff4;
                    font-size: 12px;
                    font-weight: 500;
                    color: #999eae;
                    margin: 18px 0 20px 30px;
                    padding: 5px 29px;
                    &:before {
                        content: '';
                        position: absolute;
                        top: -7px;
                        left: 35px;
                        width: 0;
                        height: 0;
                        border-left: 7px solid transparent;
                        border-right: 7px solid transparent;
                        border-bottom: 7px solid #00ba45;
                    }
                    &:after {
                        content: '';
                        position: absolute;
                        top: -5px;
                        left: 35px;
                        width: 0;
                        height: 0;
                        border-left: 7px solid transparent;
                        border-right: 7px solid transparent;
                        border-bottom: 7px solid #eefff4;
                    }
                }
            }
            
            .btn {
                margin-left: 10px;
            }
        }
        .table-box {
            margin: 20px 0;
            .table-btn {
                cursor: pointer;
                color: #409EFF;
                font-size: 13px;
                font-weight: 500;
                &:hover {
                    text-decoration: underline;
                }
            }
        }
        .tools {
            padding-right: 20px;
            display: flex;
            justify-content: flex-end;
        }
    }
</style>
